package com.example.demojpa.service;
import com.example.demojpa.dao.CourseDao;
import com.example.demojpa.entity.Course;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
@Service
public class CourseService {
    @Autowired
    CourseDao courseDao;
    public CourseService() {
    }
    public List<Course> getAllCourses() {
        return this.courseDao.findAll();
    }
    public Course findCourseByCname(String cname) {
        return this.courseDao.findByCname(cname);
    }
    public Course saveCourse(Course course) {
        return (Course)this.courseDao.save(course);
    }
    public void updateCourseNameByCno(Integer cno, String newCname) {
        Course course = this.courseDao.findByCno(cno);
        if (course != null) {
            course.setCname(newCname);
            this.courseDao.save(course);
        } else {
            throw new IllegalArgumentException("Course with cno " + cno + " does not exist.");
        }
    }
    public void deleteCourseById(Integer cno) {
        this.courseDao.deleteById(cno);
    }
    public Page<Course> findCoursesByPage(int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        return this.courseDao.findAll(pageable);
    }
}